package com.gmrz.uaf.server;

import com.gmrz.uaf.common.GuiceUtil;
import com.gmrz.uaf.common.TrustedFacets;
import com.gmrz.uaf.common.version;
import com.gmrz.uaf.db.DAOException;
import com.gmrz.uaf.db.DAOFactory;
import com.gmrz.uaf.db.UAFDAOFactory;
import com.gmrz.uaf.db.UAFDBConnectionMgr;
import com.gmrz.uaf.db.dao.UasConfigDAO;
import com.gmrz.util.db.DBUtil;
import com.google.common.base.Strings;
import com.google.gson.Gson;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class FacetsServlet extends HttpServlet {

    private static final Logger LOG = LogManager.getLogger(FacetsServlet.class);

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        long adddaysM = 300 * 1000;
        response.setHeader("Content-type", "application/fido.trusted-apps+json;text/html; charset=UTF-8");
        response.setHeader("Accept","application/fido.trusted-apps+json;text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        request.setCharacterEncoding("UTF-8");   //设置编码
        response.addDateHeader("Last-Modified", System.currentTimeMillis());

        response.setDateHeader("Expires",-1);
        //response.setDateHeader("Expires", System.currentTimeMillis()+adddaysM);
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        // response.setDateHeader("Expires",-1);

        String tenantid = request.getParameter("tenantid");
        String url = request.getRequestURL().toString();
        int end = url.lastIndexOf("/");
        String urlTemp = url.substring(0,end);
        int start = urlTemp.lastIndexOf("/");
        tenantid = urlTemp.substring(start+1);
        LOG.debug("appIdurl:"+url+",tenantid:"+tenantid);
        // String facetsval="";
        List facetsval=new ArrayList();
        Connection conn = null;
        if(Strings.isNullOrEmpty(tenantid)){
            tenantid="default";
        }

        HashMap<String,List<TrustedFacets>> jsonStr=new HashMap<String, List<TrustedFacets>>();

        TrustedFacets person = new TrustedFacets();
        version verstr=new version();
        verstr.setMajor(1);
        verstr.setMinor(0);
        person.setVersion(verstr);

        ArrayList idsLst = new ArrayList();
        try {
            conn = UAFDBConnectionMgr.getConnection(false);
//            UasConfigDAO facetsDAO = GuiceUtil.getProcessorInjector().getInstance(DAOFactory.class)
//                    .createUasConfigDAO(conn);
            UasConfigDAO facetsDAO = UAFDAOFactory.createUasConfigDAO(conn);
            facetsval = facetsDAO.loadfacetsServlet(tenantid);
        } catch (SQLException e) {
            LOG.error("facet.id the database data read failure. ", e);
            e.printStackTrace();
        } catch (DAOException e) {
            LOG.error("facet.id the database data read failure. ", e);
            e.printStackTrace();
        } finally {
            DBUtil.close(conn);
        }

        //  idsLst.add(facetsval);
        // idsLst.add("com.noknok.android.sampleapp");
        person.setIds(facetsval);

        List<TrustedFacets> PerLst=new ArrayList<TrustedFacets>();
        PerLst.add(person);

        //javabean转换成json字符串
        jsonStr.put("trustedFacets",PerLst);

        Gson gson = new Gson();
        String jsonStr1 = gson.toJson(jsonStr);

        response.getOutputStream().write(jsonStr1.getBytes());
    }
}